package com.magic.gre.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.magic.gre.R;
import com.magic.gre.callback.OnBannerItemClickListener;
import com.magic.gre.entity.ThesaurusBean;
import com.noname.lib_base_java.glide.LoadImageUtils;
import com.noname.lib_base_java.util.L;
import com.noname.lib_base_java.util.PathUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2019-01-16 16:41
 * 文件名: ThesaurusAdapter
 * 描述:
 */
public class ThesaurusAdapter extends PagerAdapter {
    private RequestOptions requestOptions = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL);
    private Context context;
    private List<ThesaurusBean> dataList = new ArrayList<>();
    private LayoutInflater mInflater;
    private OnBannerItemClickListener<ThesaurusBean> onBannerItemClickListener;

    public ThesaurusAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<ThesaurusBean> dataList) {
        this.dataList.clear();
        this.dataList.addAll(dataList);
    }

    public void setOnBannerItemClickListener(OnBannerItemClickListener<ThesaurusBean> onBannerItemClickListener) {
        this.onBannerItemClickListener = onBannerItemClickListener;
    }

    @Override
    public int getCount() {
        return dataList == null ? 0 : dataList.size();
    }


    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        final ThesaurusBean t = dataList.get(position);
        View view = mInflater.inflate(R.layout.item_thesaurus, container, false);

        ImageView imageView = view.findViewById(R.id.mImageView);
        LoadImageUtils.loadImage(PathUtil.imgPath(t.getBannerData().getPath()), imageView,requestOptions);

        TextView tv1 = view.findViewById(R.id.tv1);
        TextView tv2 = view.findViewById(R.id.tv2);
        String color = t.getColor();
        if (color!=null&& TextUtils.isEmpty(color)){
            tv1.setTextColor(Color.parseColor(t.getColor()));
            tv2.setTextColor(Color.parseColor(t.getColor()));
        }

        String totalStr = String.valueOf(t.getTotal());
        String strHint = " /";
        String numStr = String.valueOf(t.getNum());
        String content1 = numStr + strHint + totalStr + " 词";

        int start1 = content1.indexOf(numStr);
        int end1 = start1 + numStr.length();

        int start2 = content1.indexOf(strHint);
        int end2 = start2 + strHint.length();

        SpannableString styledText1 = new SpannableString(content1);
        styledText1.setSpan(new TextAppearanceSpan(context, R.style.ThesaurusBigStyle1), start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText1.setSpan(new TextAppearanceSpan(context, R.style.ThesaurusBigStyle2), start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv1.setText(styledText1, TextView.BufferType.SPANNABLE);

        String countStr = "0";
        String toDayNum = "0";
        String hintStr = " 今日背词数量 ";
        String content2 = "遍数" + countStr + " 今日背词数量 " + toDayNum;

        int start3 = content2.indexOf(countStr);
        int end3 = start3 + countStr.length();

        int start4 = content2.indexOf(toDayNum, content2.indexOf(hintStr));
        int end4 = start4 + toDayNum.length();

        SpannableString styledText2 = new SpannableString(content2);
        styledText2.setSpan(new TextAppearanceSpan(context, R.style.ThesaurusBigStyle3), start3, end3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText2.setSpan(new TextAppearanceSpan(context, R.style.ThesaurusBigStyle3), start4, end4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv2.setText(styledText2, TextView.BufferType.SPANNABLE);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBannerItemClickListener != null) {
                    onBannerItemClickListener.onBannerItemClick(t, position);
                }
            }
        });
        container.addView(view);

        return view;
    }
}
